package dice.sinanya.tools.checkdata;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

import java.util.regex.Pattern;

public class CheckCosInNet {
    static Pattern net1 = Pattern.compile("10\\.[0-9]+\\.[0-9]+\\.[0-9]+");
    static Pattern net2 = Pattern.compile("100\\.[0-9]+\\.[0-9]+\\.[0-9]+");
    static Pattern net3 = Pattern.compile("169\\.254\\.[0-9]+\\.[0-9]+");

    public static boolean checkCostInNet(String updateUrl) throws TextParseException {
        boolean result = false;
        //查询域名对应的IP地址
        Lookup lookup = new Lookup(updateUrl, Type.A);
        lookup.run();
        if (lookup.getResult() != Lookup.SUCCESSFUL) {
            return false;
        }
        Record[] answers = lookup.getAnswers();
        for (Record rec : answers) {
            result = result || net1.matcher(rec.toString()).find();
            result = result || net2.matcher(rec.toString()).find();
            result = result || net3.matcher(rec.toString()).find();
        }
        return result;
    }
}
